package com.aishua.fire.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aishua.fire.common.ExcelUtil;
import com.aishua.fire.common.constant.ExcelConstants;
import com.aishua.fire.entity.app.AppCheckItemDetailsInfo;
import com.aishua.fire.entity.app.AppCheckPlanDetailsInfo;
import com.aishua.fire.entity.app.AppCheckPlanRespInfo;
import com.aishua.fire.entity.app.AppCheckPointDetailsInfo;
import com.aishua.fire.entity.checkManager.CheckItemRespInfo;
import com.aishua.fire.entity.checkManager.CheckPlanInfo;
import com.aishua.fire.entity.checkManager.CheckPlanReqInfo;
import com.aishua.fire.entity.checkManager.CheckPlanRespUserInfo;
import com.aishua.fire.entity.checkManager.CheckPlanSatisticalInfo;
import com.aishua.fire.entity.checkManager.CheckPointInfo;
import com.aishua.fire.entity.checkManager.CheckPointReqInfo;
import com.aishua.fire.entity.checkManager.CheckPointRespInfo;
import com.aishua.fire.entity.checkManager.CheckPointRespRouteInfo;
import com.aishua.fire.entity.checkManager.CheckPointSatisticalInfo;
import com.aishua.fire.entity.checkManager.CheckRecordDetailsTwoListRespInfo;
import com.aishua.fire.entity.checkManager.CheckRecordDetailsTwoRespInfo;
import com.aishua.fire.entity.checkManager.CheckRecordRespInfo;
import com.aishua.fire.entity.checkManager.CheckRouteInfo;
import com.aishua.fire.entity.checkManager.CheckRouteReqInfo;
import com.aishua.fire.mapper.CheckManagerMapper;
import com.aishua.fire.service.CheckManagerService;
import com.github.pagehelper.PageHelper;



@Service
public class CheckManagerServiceImpl implements CheckManagerService {

	@Autowired
	private CheckManagerMapper checkManagerMapper;


	@Autowired
	private ExcelUtil excelUtil;
	
	@Override
	public void addCheckPlan(CheckPlanReqInfo checkPlan) {
		checkManagerMapper.addCheckPlan(checkPlan);
		if(checkPlan.getUser_id()!=null) {
			for(String userId :checkPlan.getUser_id()) {
				Map<String, String> ma = new HashMap<String, String>();
				ma.put("user_id", userId);
				ma.put("plan_id", checkPlan.getId());
				checkManagerMapper.addPlanWithUser(ma);
			}
		}
		
	}

	
	@Override
	public void delCheckPlan(String id) {
		checkManagerMapper.delCheckPlan(id);
	}

	@Override
	public void updateCheckPlan(CheckPlanReqInfo checkPlan) {
		checkManagerMapper.updateCheckPlan(checkPlan);
		
	}

	@Override
	public List<CheckRouteInfo> searchRouteList(String route_name, String route_num, String duty_name, int pageNumber,
			int pageSize) {
		PageHelper.startPage(pageNumber, pageSize);
		Map<String, String> map = new HashMap<String, String>();
		map.put("route_name", route_name);
		map.put("route_num", route_num);
		map.put("duty_name", duty_name);
		List<CheckRouteInfo> list=checkManagerMapper.searchRouteList(map);
		for(CheckRouteInfo info :list) {
			Map<String, String> pa = new HashMap<String, String>();
			pa.put("id",info.getId() );
			List<CheckPointRespInfo> pointList = checkManagerMapper.getCheckPointInfo(pa);
			info.setPoint_id(pointList);
		}
		return list;
	}
	

	@Override
	public int searchRouteTotal(String route_name, String route_num, String duty_name) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("route_name", route_name);
		map.put("route_num", route_num);
		map.put("duty_name", duty_name);
		return checkManagerMapper.searchRouteTotal(map);
	}

	/** 新增巡检路线*/
	@Override
	public void addCheckRoute(CheckRouteReqInfo checkRoute) {
		checkManagerMapper.addCheckRoute(checkRoute);
		if(checkRoute.getPoint_id() != null) {
			for(String point : checkRoute.getPoint_id()) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("point_id", point);
				map.put("route_id",checkRoute.getId());
				checkManagerMapper.addPointRoute(map);
			}
		}
	}
	

	@Override
	public void updateCheckRoute(CheckRouteReqInfo checkRoute) {
		
		checkManagerMapper.updateCheckRoute(checkRoute);
		//先删除关联关系
		checkManagerMapper.delRouteWithPoint(checkRoute.getId());
		//添加关联关系
		if(checkRoute.getPoint_id() != null) {
			for(String point : checkRoute.getPoint_id()) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("point_id", point);
				map.put("route_id",checkRoute.getId());
				checkManagerMapper.addPointRoute(map);
			}
		}
	}

	@Override
	public void delRoute(String id) {
		checkManagerMapper.delRoute(id);
	}

	@Override
	public List<CheckPointInfo> searchPointList(String num, String name, String location, int pageNumber,
			int pageSize,String route_id) {
		PageHelper.startPage(pageNumber, pageSize);
		Map<String, String> map = new HashMap<String, String>();
		map.put("num", num);
		map.put("name", name);
		map.put("location", location);
		map.put("route_id", route_id);
		List<CheckPointInfo> list =checkManagerMapper.searchPointList(map);
		for(CheckPointInfo info : list){
			Map<String, String> pa = new HashMap<String, String>();
			pa.put("id",info.getId() );
			List<CheckItemRespInfo> itemList = checkManagerMapper.getCheckItemInfo(pa);
			info.setItem_id(itemList);
			List<CheckPointRespRouteInfo> respRouteInfo=checkManagerMapper.checkPointRespRouteInfo(pa);
			if(respRouteInfo!=null) {
				info.setRoute_id(respRouteInfo);
			}
		}
		
		return list;
	}

	@Override
	public int searchPointTotal(String num, String name, String location,String route_id) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("num", num);
		map.put("name", name);
		map.put("location", location);
		map.put("route_id", route_id);
		return checkManagerMapper.searchPointTotal(map);
	}

	@Override
	public void addCheckPoint(CheckPointReqInfo checkPoint) {
		
		checkManagerMapper.addCheckPoint(checkPoint);
		if(checkPoint.getItem_id()!=null) {
			for(String item : checkPoint.getItem_id()) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("item_id", item);
				map.put("point_id", checkPoint.getId());
				checkManagerMapper.addPointItem(map);
			}	
			if(checkPoint.getRoute_id()!=null) {
				for(String route_id : checkPoint.getRoute_id()) {
				Map<String, String> m = new HashMap<String, String>();
				m.put("point_id", checkPoint.getId());
				m.put("route_id",route_id);
				checkManagerMapper.addPointRoute(m);
			}	
			}
			
		}
	}
	
	

	@Override
	public void delPoint(String id) {
		checkManagerMapper.delPoint(id);	
	}

	@Override
	public void updateCheckPoint(CheckPointReqInfo checkPoint) {
		checkManagerMapper.updateCheckPoint(checkPoint);
		if(checkPoint.getItem_id()!=null) {
			for(String item : checkPoint.getItem_id()) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("item_id", item);
				map.put("id", checkPoint.getId());
				checkManagerMapper.updatePointItem(map);
			}
			//先刪除关联
			checkManagerMapper.delPonitWithRoute(checkPoint.getId());
			//新增加关联
			if(checkPoint.getRoute_id()!=null) {
				for(String route_id : checkPoint.getRoute_id()) {
				Map<String, String> m = new HashMap<String, String>();
				m.put("point_id", checkPoint.getId());
				m.put("route_id",route_id);
				checkManagerMapper.addPointRoute(m);
			}	
			}
		}
		
		
	}

	/**
	 * 查询巡检计划统计分析列表
	 */
	@Override
	public List<CheckPlanSatisticalInfo> planStatisticalList(String start_time, String end_time, String duty_name,
			String name, int pageNumber, int pageSize)
	{
		PageHelper.startPage(pageNumber, pageSize);
		Map<String, String> map = new HashMap<String, String>();
		map.put("start_time", start_time);
		map.put("end_time", end_time);
		map.put("duty_name", duty_name);
		map.put("name", name);
		List<CheckPlanSatisticalInfo> list = checkManagerMapper.getPlanStatisticalList(map);
		for(CheckPlanSatisticalInfo info : list)
		{
			info.setActual_num("0");
			info.setComplete_num("0");
			info.setLeave_num("0");
			info.setLeave_rate("0");
			info.setPlan_num("0");
			info.setUnqualified_num("0");
			info.setUnqualified_rate("0");
		}
		
		return list;
	}

	/**
	 * 查询巡检计划统计分析列表数量
	 */
	@Override
	public int planStatisticalTotal(String start_time, String end_time, String duty_name, String name) 
	{
		Map<String, String> map = new HashMap<String, String>();
		map.put("start_time", start_time);
		map.put("end_time", end_time);
		map.put("duty_name", duty_name);
		map.put("name", name);
		int total = checkManagerMapper.planStatisticalTotal(map);
		return total;
	}

	@Override
	public List<CheckPointSatisticalInfo> pointStatisticalList(String start_time, String end_time, String duty_name,
			String point_name, int pageNumber, int pageSize) {
		PageHelper.startPage(pageNumber, pageSize);
		Map<String, String> map = new HashMap<String, String>();
		map.put("start_time", start_time);
		map.put("end_time", end_time);
		map.put("duty_name", duty_name);
		map.put("point_name", point_name);
		List<CheckPointSatisticalInfo> list = checkManagerMapper.getPointStatisticalList(map);
		return list;
	}

	@Override
	public int pointStatisticalTotal(String start_time, String end_time, String duty_name, String point_name) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("start_time", start_time);
		map.put("end_time", end_time);
		map.put("duty_name", duty_name);
		map.put("point_name", point_name);
		return checkManagerMapper.getPointStatisticalTotal(map);
	}

	@Override
	public List<CheckPlanInfo> searchPlanList(String num, String name, String type,
			int pageNumber, int pageSize) {
		PageHelper.startPage(pageNumber, pageSize);
		Map<String, String> map = new HashMap<String, String>();
		map.put("num", num);
		map.put("name", name);
		map.put("type", type);
		List<CheckPlanInfo> list =checkManagerMapper.searchPlanList(map);
		for(CheckPlanInfo info :list) {
			Map<String, String> pa = new HashMap<String, String>();
			pa.put("id",info.getId() );
			List<CheckPlanRespUserInfo> userList = checkManagerMapper.getCheckPlanByUserInfo(pa);
			info.setUser_id(userList);
		}
		return list;
	}

	@Override
	public int searchPlanTotal(String num, String name, String type) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("num", num);
		map.put("name", name);
		map.put("type", type);
		return checkManagerMapper.searchPlanTotal(map);
	}

	@Override
	public void updatePointImg(String picName, String num) {
		checkManagerMapper.updatePointImg(picName,num);
	}


	@Override
	public boolean findByPointNum(String num) {
		List<String> nums =checkManagerMapper.findByPointNum(num);
		if(nums.size() > 0)
		{
			return false;
		}
		return true;
	}


	@Override
	public boolean findByRouteNum(String route_num) {
		List<String> num =checkManagerMapper.findByRouteNum(route_num);
		if(num.size() > 0)
		{
			return false;
		}
		return true;
	}


	@Override
	public boolean findByPlanNum(String num) {
		List<String> nums =checkManagerMapper.findByPlanNum(num);
		if(nums.size() > 0)
		{
			return false;
		}
		return true;
	}


	@Override
	public List<CheckRecordRespInfo> searchRecordList(String userName, String pointName, String situation,
			String start_time, String end_time, int pageNumber, int pageSize) {
		PageHelper.startPage(pageNumber, pageSize);
		Map<String, String> map = new HashMap<String, String>();
		map.put("userName", userName);
		map.put("pointName", pointName);
		map.put("situation", situation);
		map.put("start_time", start_time);
		map.put("end_time", end_time);
		return checkManagerMapper.searchRecordList(map);
	}


	@Override
	public String exportRecord(String [] ids) {
		 List<CheckRecordRespInfo> list =checkManagerMapper.exportRecord(ids);
		return excelUtil.checkRecordExcel(list,ExcelConstants.EXCEL_TEMPPATH,ExcelConstants.EXCEL_CHECK);
	}


	@Override
	public List<AppCheckPlanRespInfo> searchPlanListByApp(int pageNumber, int pageSize) {
		PageHelper.startPage(pageNumber, pageSize);
		List<AppCheckPlanRespInfo> list =checkManagerMapper.searchPlanListByApp();
		return list;
	}


	@Override
	public AppCheckPlanDetailsInfo checkPlanDetailsByApp(String id) {
		AppCheckPlanDetailsInfo planDetails= checkManagerMapper.checkPlanDetailsByApp(id);
		 //查询巡检点
		planDetails.setPointInfo(checkManagerMapper.checkPointByApp(planDetails.getRoute_id()));
		planDetails.setUser_id(checkManagerMapper.checkUserInfoByapp(planDetails.getId()));
		 return planDetails;
	}


	@Override
	public AppCheckPointDetailsInfo checkPointDetailsByApp(String id) {
		AppCheckPointDetailsInfo pointDetails =checkManagerMapper.checkPointDetailsByApp(id);
		//查询巡检项目
		pointDetails.setItemInfo(checkManagerMapper.checkItemByApp(pointDetails.getId()));;
		return pointDetails;
	}


	@Override
	public AppCheckItemDetailsInfo checkItemDetailsByApp(String id) {
		return checkManagerMapper.checkItemDetailsByApp(id);
	}
	
    /**
     * 查询巡检记录详情接口
     */
    @Override
    public CheckRecordDetailsTwoRespInfo recordDetails(String r_id) 
    {
        CheckRecordDetailsTwoRespInfo bean = new CheckRecordDetailsTwoRespInfo();
        
        bean = checkManagerMapper.getRecordDetails(r_id);
        
        List<CheckRecordDetailsTwoListRespInfo> details = checkManagerMapper.getrecordItemDetails(r_id);
        bean.setDetails(details);
        return bean;
    }
}
